package org.example.neusoft.app;


import org.example.neusoft.model.User;
import org.example.neusoft.util.GlobalInput;
import org.example.neusoft.view.ArticleView;
import org.example.neusoft.view.UserView;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        boolean isRunning = true;
        Scanner scanner = GlobalInput.getScanner();
        while (isRunning){
            System.out.println("-----RealWorld-----");
            if(User.loginUser == null){
                System.out.println("1.用户注册");
                System.out.println("2.用户登录");
                System.out.println("3.查看文章");
                System.out.println("0.退出系统");
            }
            else{
                System.out.println("1.发表文章");
                System.out.println("2.修改个人信息");
                System.out.println("3.查看文章");
                System.out.println("4.退出登录");
                System.out.println("0.退出系统");
            }

            int cmd = scanner.nextInt();
            scanner.nextLine();
            switch (cmd){
                case 1:{
                    if(User.loginUser == null){
                        UserView userView = new UserView();
                        User.loginUser = userView.regUser();
                        if(User.loginUser != null){
                            System.out.println("注册成功");
                        }
                        else{
                            System.out.println("注册失败");
                        }
                    }
                    else{
                        ArticleView articleView = new ArticleView();
                        articleView.createArticle();
                    }

                    break;
                }
                case 2:{
                    UserView userView = new UserView();
                    User.loginUser  = userView.loginUser();
                    if(User.loginUser != null){
                        System.out.println("登录成功");
                    }
                    else{
                        System.out.println("登录失败");
                    }
                    break;
                }
                case 3:{
                    while (true){
                        ArticleView articleView = new ArticleView();
                        List<Map<String,String>> mapLst = articleView.getAllArticleLst();
                        int index = 0;
                        for(Map<String,String> map : mapLst){
                            System.out.println((index+1) + "." + map.get("title")+" "
                                    +map.get("username")+" " +map.get("taglist"));
                            index++;
                        }
                        System.out.println("输入文章编号，查看详情\n 输入0返回上一层");
                        int article_id = scanner.nextInt();
                        scanner.nextLine();
                        if(article_id > 0){
                            viewArticle(article_id, mapLst);
                        }
                        else{
                            break;
                        }
                    }
                    break;
                }
                case 4:{
                    User.loginUser = null;
                    System.out.println("退出登录成功");
                    break;
                }
                case 0:{
                    isRunning = false;
                    break;
                }
                default:{
                    break;
                }
            }
        }

        scanner.close();
        System.out.println("byebye");
    }

    private static void viewArticle(int article_id
            , List<Map<String, String>> mapLst) {
        Scanner scanner = GlobalInput.getScanner();
        boolean isRun = true;
        while (isRun){
            Map<String,String> map = mapLst.get(article_id-1);
            System.out.println(map.get("title"));
            System.out.println(map.get("body"));
            System.out.println(map.get("created_at"));
            System.out.println(map.get("username"));
            System.out.println(map.get("taglist"));

            System.out.println("0. 返回到上一层");
            System.out.println("1. 留言");
            System.out.println("2. 点赞");

            int command = scanner.nextInt();
            scanner.nextLine();
            switch (command){
                case 0:{
                    isRun = false;
                    break;
                }
                case 1:{
                    System.out.println("请输入留言内容");
                    String content = scanner.nextLine();
                    ArticleView articleView = new ArticleView();
                    if(User.loginUser != null){
                        articleView.takeCommand(content,map.get("id"),User.loginUser.getId());
                    }
                    break;
                }
                case 2:{
                    ArticleView articleView = new ArticleView();
                    if(User.loginUser != null){
                        articleView.takeFavorite(map.get("id"),User.loginUser.getId());
                    }
                    break;
                }
                default:{
                    break;
                }
            }
        }


    }
}

